import axios from "axios";
//进度条
import Nprogress from "nprogress";
import "nprogress/nprogress.css";
const request = axios.create({
  baseURL: "/api",
  timeout: 2000,
});

//设置请求拦截器
request.interceptors.request.use(
  function (config) {
    Nprogress.start();
    //携带token
    config.headers.token = localStorage.getItem("token") || "";
    // config.headers.transformRequest = [(data) => data];
    return config;
  },
  function (err) {
    Nprogress.done();
    //返回失败结果
    return Promise.reject(err);
  }
);
//响应拦截器
request.interceptors.response.use(
  function (response) {
    Nprogress.done();
    return response.data;
  },
  function (err) {
    return Promise.reject(err);
  }
);

export default request;
